/*     */ package com.zimbra.cs.dav.resource;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZComponent;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavException.InvalidData;
/*     */ import com.zimbra.cs.dav.DavException.NeedSameOrganizerInAllComponents;
/*     */ import com.zimbra.cs.dav.caldav.AutoScheduler;
/*     */ import com.zimbra.cs.dav.caldav.CalDavUtils;
/*     */ import com.zimbra.cs.dav.caldav.Range.TimeRange;
/*     */ import com.zimbra.cs.dav.property.CalDavProperty;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import com.zimbra.cs.dav.service.DavServlet;
/*     */ import com.zimbra.cs.fb.FreeBusy;
/*     */ import com.zimbra.cs.fb.FreeBusy.Method;
/*     */ import com.zimbra.cs.mailbox.BadOrganizerException;
/*     */ import com.zimbra.cs.mailbox.BadOrganizerException.DiffOrganizerInComponentsException;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.CalendarMetadata;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.ReplyInfo;
/*     */ import com.zimbra.cs.mailbox.DavNames;
/*     */ import com.zimbra.cs.mailbox.DavNames.DavName;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.mailbox.calendar.cache.CtagInfo;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.util.AccountUtil.AccountAddressMatcher;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.net.URLDecoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CalendarCollection
/*     */   extends Collection
/*     */ {
/*     */   public CalendarCollection(DavContext ctxt, Folder f)
/*     */     throws DavException, ServiceException
/*     */   {
/*  94 */     super(ctxt, f);
/*  95 */     Account acct = f.getAccount();
/*     */     
/*  97 */     if ((f.getDefaultView() == MailItem.Type.APPOINTMENT) || (f.getDefaultView() == MailItem.Type.TASK)) {
/*  98 */       addResourceType(DavElements.E_CALENDAR);
/*     */     }
/*     */     
/*     */ 
/* 102 */     Locale lc = acct.getLocale();
/* 103 */     String description = L10nUtil.getMessage(L10nUtil.MsgKey.caldavCalendarDescription, lc, new Object[] { acct.getAttr("displayName"), f.getName() });
/* 104 */     ResourceProperty desc = new ResourceProperty(DavElements.E_CALENDAR_DESCRIPTION);
/* 105 */     desc.setMessageLocale(lc);
/* 106 */     desc.setStringValue(description);
/* 107 */     desc.setVisible(false);
/* 108 */     addProperty(desc);
/* 109 */     addProperty(CalDavProperty.getSupportedCalendarComponentSet(f.getDefaultView()));
/* 110 */     addProperty(CalDavProperty.getSupportedCalendarData());
/* 111 */     addProperty(CalDavProperty.getSupportedCollationSet());
/* 112 */     addProperty(CalDavProperty.getCalendarTimezone(acct));
/*     */     
/* 114 */     this.mCtag = CtagInfo.makeCtag(f);
/* 115 */     setProperty(DavElements.E_GETCTAG, this.mCtag);
/*     */     
/* 117 */     addProperty(getIcalColorProperty());
/* 118 */     setProperty(DavElements.E_ALTERNATE_URI_SET, null, true);
/* 119 */     setProperty(DavElements.E_GROUP_MEMBER_SET, null, true);
/* 120 */     setProperty(DavElements.E_GROUP_MEMBERSHIP, null, true);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 127 */   private static final QName[] SUPPORTED_REPORTS = { DavElements.E_CALENDAR_QUERY, DavElements.E_CALENDAR_MULTIGET, DavElements.E_FREE_BUSY_QUERY, DavElements.E_ACL_PRINCIPAL_PROP_SET, DavElements.E_PRINCIPAL_MATCH, DavElements.E_PRINCIPAL_PROPERTY_SEARCH, DavElements.E_PRINCIPAL_SEARCH_PROPERTY_SET, DavElements.E_EXPAND_PROPERTY };
/*     */   
/*     */ 
/*     */   protected Map<String, DavResource> mAppts;
/*     */   
/*     */ 
/*     */   protected boolean mMetadataOnly;
/*     */   
/*     */   protected String mCtag;
/*     */   
/*     */ 
/*     */   protected QName[] getSupportedReports()
/*     */   {
/* 140 */     return SUPPORTED_REPORTS;
/*     */   }
/*     */   
/*     */   public java.util.Collection<DavResource> getChildren(DavContext ctxt)
/*     */     throws DavException
/*     */   {
/* 146 */     return getChildren(ctxt, null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public java.util.Collection<DavResource> getChildren(DavContext ctxt, Range.TimeRange range)
/*     */     throws DavException
/*     */   {
/* 155 */     Map<String, DavResource> requestedAppts = null;
/* 156 */     boolean fetchAppts = (range != null) || (this.mAppts == null);
/*     */     
/*     */ 
/* 159 */     if (fetchAppts) {
/*     */       try {
/* 161 */         requestedAppts = getAppointmentMap(ctxt, range);
/*     */         
/* 163 */         if ((range == null) && (needCalendarData(ctxt)))
/* 164 */           this.mAppts = requestedAppts;
/*     */       } catch (ServiceException se) {
/* 166 */         ZimbraLog.dav.error("can't get calendar items", se);
/* 167 */         return Collections.emptyList();
/*     */       }
/*     */     } else {
/* 170 */       requestedAppts = this.mAppts;
/*     */     }
/* 172 */     return requestedAppts.values();
/*     */   }
/*     */   
/*     */   protected Map<String, String> getUidToHrefMap(java.util.Collection<String> hrefs) {
/* 176 */     HashMap<String, String> uidmap = new HashMap();
/* 177 */     for (String href : hrefs) {
/*     */       try {
/* 179 */         int start = href.lastIndexOf('/') + 1;
/* 180 */         int end = href.lastIndexOf(".ics");
/* 181 */         if ((start >= 0) && (end > start)) {
/* 182 */           String uid = href.substring(start, end);
/* 183 */           uid = URLDecoder.decode(uid, "UTF-8");
/* 184 */           if ((start > 0) && (end > 0) && (end > start)) {
/* 185 */             uidmap.put(uid, href);
/*     */           }
/*     */         } else {
/* 188 */           ZimbraLog.dav.warn("Unexpected href '%s' for a calendar item - ignoring it.", new Object[] { href });
/*     */         }
/*     */       } catch (Exception e) {
/* 191 */         ZimbraLog.dav.warn("can't decode href %s", href, e);
/*     */       }
/*     */     }
/* 194 */     return uidmap;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 201 */   private static final HashSet<QName> sMetaProps = new HashSet();
/* 202 */   static { sMetaProps.add(DavElements.E_GETETAG);
/* 203 */     sMetaProps.add(DavElements.E_RESOURCETYPE);
/* 204 */     sMetaProps.add(DavElements.E_DISPLAYNAME);
/*     */   }
/*     */   
/* 207 */   protected boolean needCalendarData(DavContext ctxt) throws DavException { String method = ctxt.getRequest().getMethod();
/* 208 */     if ((method.equals("GET")) || (method.equals("DELETE")))
/* 209 */       return true;
/* 210 */     for (QName prop : ctxt.getRequestProp().getProps())
/* 211 */       if (!sMetaProps.contains(prop))
/* 212 */         return true;
/* 213 */     return false;
/*     */   }
/*     */   
/* 216 */   protected Mailbox getCalendarMailbox(DavContext ctxt) throws ServiceException, DavException { return getMailbox(ctxt); }
/*     */   
/*     */   protected Map<String, DavResource> getAppointmentMap(DavContext ctxt, Range.TimeRange range) throws ServiceException, DavException {
/* 219 */     Mailbox mbox = getCalendarMailbox(ctxt);
/*     */     
/* 221 */     HashMap<String, DavResource> appts = new HashMap();
/* 222 */     ctxt.setCollectionPath(getUri());
/* 223 */     if (range == null)
/* 224 */       range = new Range.TimeRange(getOwner());
/* 225 */     long start = range.getStart();
/* 226 */     long end = range.getEnd();
/* 227 */     start = start == Long.MIN_VALUE ? -1L : start;
/* 228 */     end = end == Long.MAX_VALUE ? -1L : end;
/* 229 */     if (!needCalendarData(ctxt)) {
/* 230 */       ZimbraLog.dav.debug("METADATA only");
/* 231 */       this.mMetadataOnly = true;
/* 232 */       for (CalendarItem.CalendarMetadata item : mbox.getCalendarItemMetadata(getId(), start, end)) {
/* 233 */         appts.put(item.uid, new CalendarObject.LightWeightCalendarObject(getUri(), getOwner(), item));
/*     */       }
/*     */     } else {
/* 236 */       for (CalendarItem calItem : mbox.getCalendarItemsForRange(ctxt.getOperationContext(), start, end, getId(), null))
/* 237 */         appts.put(calItem.getUid(), new CalendarObject.LocalCalendarObject(ctxt, calItem));
/*     */     }
/* 239 */     return appts;
/*     */   }
/*     */   
/*     */   public java.util.Collection<DavResource> getAppointmentsByUids(DavContext ctxt, List<String> hrefs) throws ServiceException, DavException {
/* 243 */     Map<String, String> uidmap = getUidToHrefMap(hrefs);
/* 244 */     Mailbox mbox = getCalendarMailbox(ctxt);
/*     */     
/* 246 */     ArrayList<DavResource> appts = new ArrayList();
/* 247 */     ctxt.setCollectionPath(getUri());
/* 248 */     Map<String, CalendarItem> calItems = mbox.getCalendarItemsByUid(ctxt.getOperationContext(), new ArrayList(uidmap.keySet()));
/* 249 */     for (String uid : calItems.keySet()) {
/* 250 */       CalendarItem calItem = (CalendarItem)calItems.get(uid);
/* 251 */       if (calItem == null) {
/* 252 */         appts.add(new DavResource.InvalidResource((String)uidmap.get(uid), getOwner()));
/*     */       } else
/* 254 */         appts.add(new CalendarObject.LocalCalendarObject(ctxt, calItem));
/*     */     }
/* 256 */     return appts;
/*     */   }
/*     */   
/*     */   private String findSummary(ZCalendar.ZVCalendar cal) {
/* 260 */     Iterator<ZCalendar.ZComponent> iter = cal.getComponentIterator();
/* 261 */     while (iter.hasNext()) {
/* 262 */       ZCalendar.ZComponent comp = (ZCalendar.ZComponent)iter.next();
/* 263 */       String summary = comp.getPropVal(ZCalendar.ICalTok.SUMMARY, null);
/* 264 */       if (summary != null)
/* 265 */         return summary;
/*     */     }
/* 267 */     return "calendar event";
/*     */   }
/*     */   
/*     */   private String findEventUid(List<Invite> invites) throws DavException {
/* 271 */     String uid = null;
/* 272 */     MailItem.Type itemType = null;
/* 273 */     LinkedList<Invite> inviteList = new LinkedList();
/* 274 */     for (Invite i : invites) {
/* 275 */       MailItem.Type mItemType = i.getItemType();
/* 276 */       if ((mItemType == MailItem.Type.APPOINTMENT) || (mItemType == MailItem.Type.TASK)) {
/* 277 */         if ((uid != null) && (uid.compareTo(i.getUid()) != 0))
/* 278 */           throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_OBJECT_RESOURCE, "too many components");
/* 279 */         uid = i.getUid();
/*     */       }
/*     */       
/* 282 */       if ((itemType != null) && (itemType != mItemType)) {
/* 283 */         throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_OBJECT_RESOURCE, "different types of components in the same resource");
/*     */       }
/* 285 */       itemType = mItemType;
/*     */       
/* 287 */       if (i.isRecurrence()) {
/* 288 */         inviteList.addFirst(i);
/*     */       } else
/* 290 */         inviteList.addLast(i);
/*     */     }
/* 292 */     if (uid == null) {
/* 293 */       throw new DavException.InvalidData(DavElements.E_SUPPORTED_CALENDAR_COMPONENT, "no event in the request");
/*     */     }
/* 295 */     if (((getDefaultView() == MailItem.Type.APPOINTMENT) || (getDefaultView() == MailItem.Type.TASK)) && (itemType != getDefaultView())) {
/* 296 */       throw new DavException.InvalidData(DavElements.E_SUPPORTED_CALENDAR_COMPONENT, "resource type not supported in this collection");
/*     */     }
/* 298 */     invites.clear();
/* 299 */     invites.addAll(inviteList);
/* 300 */     return uid;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void adjustOrganizer(DavContext ctxt, Invite invite)
/*     */     throws ServiceException
/*     */   {
/* 310 */     if ((!invite.hasOrganizer()) && (!invite.hasOtherAttendees())) {
/* 311 */       return;
/*     */     }
/* 313 */     ZOrganizer org = invite.getOrganizer();
/* 314 */     String origOrganizerAddr = null;
/*     */     
/* 316 */     if (org == null) {
/* 317 */       org = new ZOrganizer(ctxt.getAuthAccount().getName(), null);
/* 318 */       invite.setOrganizer(org);
/* 319 */       invite.setIsOrganizer(true);
/*     */     }
/*     */     else {
/* 322 */       origOrganizerAddr = org.getAddress();
/* 323 */       Account acct = Provisioning.getInstance().get(Key.AccountBy.name, origOrganizerAddr);
/* 324 */       if (acct != null) {
/* 325 */         String newAddr = acct.getName();
/* 326 */         if (!origOrganizerAddr.equals(newAddr)) {
/* 327 */           org.setAddress(newAddr);
/* 328 */           invite.setIsOrganizer(acct);
/*     */         }
/*     */       }
/*     */     }
/* 332 */     if (ctxt.useIcalDelegation()) {
/* 333 */       return;
/*     */     }
/* 335 */     String owner = getOwner();
/* 336 */     if (owner == null) {
/* 337 */       return;
/*     */     }
/* 339 */     Account ownerAcct = Provisioning.getInstance().get(Key.AccountBy.name, owner);
/* 340 */     if (ownerAcct == null) {
/* 341 */       return;
/*     */     }
/* 343 */     AccountUtil.AccountAddressMatcher acctMatcher = new AccountUtil.AccountAddressMatcher(ctxt.getAuthAccount());
/* 344 */     origOrganizerAddr = org.getAddress();
/* 345 */     if ((acctMatcher.matches(origOrganizerAddr)) && (!owner.equalsIgnoreCase(origOrganizerAddr)))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/* 350 */       org.setAddress(ownerAcct.getName());
/* 351 */       org.setCn(ownerAcct.getDisplayName());
/* 352 */       org.setSentBy(origOrganizerAddr);
/* 353 */       invite.setIsOrganizer(ownerAcct);
/* 354 */       boolean hasOwnerAttendee = false;
/* 355 */       for (ZAttendee attendee : invite.getAttendees()) {
/* 356 */         if (attendee.addressMatches(owner)) {
/* 357 */           hasOwnerAttendee = true;
/*     */         }
/*     */       }
/* 360 */       if (!hasOwnerAttendee)
/*     */       {
/*     */ 
/* 363 */         for (ZAttendee attendee : invite.getAttendees()) {
/* 364 */           if ((attendee.addressMatches(origOrganizerAddr)) && ("AC".equalsIgnoreCase(attendee.getPartStat())))
/*     */           {
/* 366 */             attendee.setAddress(ownerAcct.getName());
/* 367 */             attendee.setCn(ownerAcct.getDisplayName());
/* 368 */             attendee.setSentBy(origOrganizerAddr);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public DavResource createItem(DavContext ctxt, String name)
/*     */     throws DavException, IOException
/*     */   {
/* 381 */     if (!ctxt.getUpload().getContentType().startsWith("text/calendar")) {
/* 382 */       throw new DavException.InvalidData(DavElements.E_SUPPORTED_CALENDAR_DATA, "not a calendar data");
/*     */     }
/* 384 */     if (ctxt.getUpload().getSize() <= 0L) {
/* 385 */       throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, "empty request");
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 395 */     HttpServletRequest req = ctxt.getRequest();
/* 396 */     String etag = req.getHeader("If-Match");
/* 397 */     boolean useEtag = etag != null;
/*     */     
/*     */ 
/*     */ 
/* 401 */     String baseName = HttpUtil.urlUnescape(name);
/* 402 */     boolean acceptableClientChosenBasename = (DebugConfig.enableDAVclientCanChooseResourceBaseName) && (baseName.equals(name));
/*     */     
/* 404 */     if (name.endsWith(".ics")) {
/* 405 */       name = name.substring(0, name.length() - ".ics".length());
/*     */       
/* 407 */       name = HttpUtil.urlUnescape(name);
/*     */     }
/*     */     
/* 410 */     Provisioning prov = Provisioning.getInstance();
/*     */     try {
/* 412 */       String user = ctxt.getUser();
/* 413 */       Account account = prov.get(Key.AccountBy.name, user);
/* 414 */       if (account == null) {
/* 415 */         throw new DavException("no such account " + user, 404, null);
/*     */       }
/*     */       List<Invite> invites;
/*     */       try {
/* 419 */         InputStream is = ctxt.getUpload().getInputStream();Throwable localThrowable2 = null;
/* 420 */         try { ZCalendar.ZVCalendar vcalendar = ZCalendar.ZCalendarBuilder.build(is, "utf-8");
/* 421 */           CalDavUtils.removeAttendeeForOrganizer(vcalendar);
/* 422 */           if (ctxt.isIcalClient()) {
/* 423 */             CalDavUtils.adjustPercentCompleteForToDos(vcalendar);
/*     */           }
/* 425 */           invites = Invite.createFromCalendar(account, findSummary(vcalendar), vcalendar, true);
/*     */         }
/*     */         catch (Throwable localThrowable1)
/*     */         {
/* 419 */           localThrowable2 = localThrowable1;throw localThrowable1;
/*     */ 
/*     */ 
/*     */         }
/*     */         finally
/*     */         {
/*     */ 
/* 426 */           if (is != null) if (localThrowable2 != null) try { is.close(); } catch (Throwable x2) { localThrowable2.addSuppressed(x2); } else is.close();
/* 427 */         } } catch (ServiceException se) { throw new DavException.InvalidData(DavElements.E_VALID_CALENDAR_DATA, "cannot parse ics");
/*     */       }
/*     */       
/* 430 */       String uid = findEventUid(invites);
/* 431 */       Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 432 */       CalendarItem origCalItem = null;
/*     */       
/* 434 */       Integer itemId = null;
/* 435 */       if (acceptableClientChosenBasename) {
/* 436 */         itemId = DavNames.get(this.mMailboxId, this.mId, baseName);
/*     */       }
/* 438 */       if (itemId != null) {
/*     */         try {
/* 440 */           MailItem mailItem = mbox.getItemById(ctxt.getOperationContext(), itemId.intValue(), MailItem.Type.UNKNOWN);
/* 441 */           if ((mailItem != null) && ((mailItem instanceof CalendarItem))) {
/* 442 */             origCalItem = (CalendarItem)mailItem;
/*     */           }
/*     */         }
/*     */         catch (ServiceException se) {}
/*     */       }
/* 447 */       if (origCalItem == null) {
/* 448 */         if (uid.equals(name)) {
/* 449 */           origCalItem = mbox.getCalendarItemByUid(ctxt.getOperationContext(), name);
/*     */         }
/*     */         else {
/* 452 */           origCalItem = mbox.getCalendarItemByUid(ctxt.getOperationContext(), uid);
/* 453 */           String redirectUrl = null;
/* 454 */           if (origCalItem != null) {
/* 455 */             if (this.mId != origCalItem.getFolderId())
/*     */             {
/* 457 */               origCalItem = null;
/*     */             }
/*     */             else
/*     */             {
/* 461 */               DavNames.DavName davName = null;
/* 462 */               if (acceptableClientChosenBasename) {
/* 463 */                 davName = DavNames.get(this.mMailboxId, origCalItem.getId());
/*     */               }
/* 465 */               if (davName != null) {
/* 466 */                 redirectUrl = urlForCalendarItem(user, davName.davBaseName);
/*     */               } else {
/* 468 */                 redirectUrl = defaultUrlForCalendarItem(user, uid);
/*     */               }
/*     */             }
/*     */           }
/* 472 */           if ((origCalItem == null) && (!DebugConfig.enableDAVclientCanChooseResourceBaseName)) {
/* 473 */             redirectUrl = defaultUrlForCalendarItem(user, uid);
/*     */           }
/* 475 */           if (redirectUrl != null)
/*     */           {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 481 */             ctxt.getResponse().sendRedirect(redirectUrl);
/* 482 */             StringBuilder wrongUrlMsg = new StringBuilder();
/* 483 */             wrongUrlMsg.append("wrong url - redirecting to:\n").append(redirectUrl);
/* 484 */             throw new DavException(wrongUrlMsg.toString(), 302, null);
/*     */           }
/*     */         }
/*     */       }
/* 488 */       if ((origCalItem == null) && (useEtag)) {
/* 489 */         throw new DavException("event not found", 404, null);
/*     */       }
/*     */       
/* 492 */       boolean isNewItem = true;
/* 493 */       if (useEtag) {
/* 494 */         String itemEtag = MailItemResource.getEtag(origCalItem);
/* 495 */         if (!itemEtag.equals(etag)) {
/* 496 */           throw new DavException(String.format("CalDAV client has stale event: event has different etag (%s) vs %s", new Object[] { itemEtag, etag }), 409);
/*     */         }
/*     */         
/*     */ 
/* 500 */         isNewItem = false;
/*     */       }
/*     */       
/*     */ 
/* 504 */       int flags = 0;String[] tags = null;List<CalendarItem.ReplyInfo> replies = null;
/* 505 */       Invite[] origInvites = null;
/* 506 */       if (origCalItem != null) {
/* 507 */         flags = origCalItem.getFlagBitmask();
/* 508 */         tags = origCalItem.getTags();
/* 509 */         replies = origCalItem.getAllReplies();
/* 510 */         origInvites = origCalItem.getInvites();
/*     */       }
/* 512 */       Mailbox.SetCalendarItemData scidDefault = new Mailbox.SetCalendarItemData();
/* 513 */       Mailbox.SetCalendarItemData[] scidExceptions = null;
/*     */       
/* 515 */       int idxExceptions = 0;
/* 516 */       boolean first = true;
/* 517 */       for (Invite i : invites)
/*     */       {
/* 519 */         if (i.getUid() == null)
/* 520 */           i.setUid(uid);
/* 521 */         adjustOrganizer(ctxt, i);
/*     */         
/*     */ 
/*     */ 
/* 525 */         ParsedMessage oldPm = null;
/* 526 */         if (origCalItem != null) {
/* 527 */           Invite oldInv = origCalItem.getInvite(i.getRecurId());
/* 528 */           if ((oldInv == null) && (i.hasRecurId()))
/*     */           {
/* 530 */             oldInv = origCalItem.getInvite((RecurId)null);
/*     */           }
/* 532 */           if (oldInv != null) {
/* 533 */             MimeMessage mmInv = origCalItem.getSubpartMessage(oldInv.getMailItemId());
/* 534 */             oldPm = mmInv != null ? new ParsedMessage(mmInv, false) : null;
/*     */           }
/*     */         }
/* 537 */         if (first) {
/* 538 */           scidDefault.invite = i;
/* 539 */           scidDefault.message = oldPm;
/* 540 */           first = false;
/*     */         } else {
/* 542 */           Mailbox.SetCalendarItemData scid = new Mailbox.SetCalendarItemData();
/* 543 */           scid.invite = i;
/* 544 */           scid.message = oldPm;
/* 545 */           if (scidExceptions == null) {
/* 546 */             scidExceptions = new Mailbox.SetCalendarItemData[invites.size() - 1];
/*     */           }
/* 548 */           scidExceptions[(idxExceptions++)] = scid;
/*     */         }
/*     */         
/*     */ 
/* 552 */         if ((!i.isOrganizer()) && (replies != null)) {
/* 553 */           ZAttendee at = i.getMatchingAttendee(account);
/* 554 */           if (at != null) {
/* 555 */             AccountUtil.AccountAddressMatcher acctMatcher = new AccountUtil.AccountAddressMatcher(account);
/* 556 */             CalendarItem.ReplyInfo newReply = null;
/* 557 */             for (Iterator<CalendarItem.ReplyInfo> replyIter = replies.iterator(); replyIter.hasNext();) {
/* 558 */               CalendarItem.ReplyInfo reply = (CalendarItem.ReplyInfo)replyIter.next();
/* 559 */               if (acctMatcher.matches(reply.getAttendee().getAddress())) {
/* 560 */                 RecurId ridR = reply.getRecurId();RecurId ridI = i.getRecurId();
/* 561 */                 if (((ridR == null) && (ridI == null)) || ((ridR != null) && (ridR.equals(ridI))))
/*     */                 {
/*     */ 
/*     */ 
/*     */ 
/* 566 */                   replyIter.remove();
/* 567 */                   if ("NE".equalsIgnoreCase(at.getPartStat())) break;
/* 568 */                   newReply = new CalendarItem.ReplyInfo(at, i.getSeqNo(), i.getDTStamp(), ridI); break;
/*     */                 }
/*     */               }
/*     */             }
/*     */             
/*     */ 
/* 574 */             if (newReply != null) {
/* 575 */               replies.add(newReply);
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/* 580 */       CalendarItem newCalItem = null;
/* 581 */       AutoScheduler autoScheduler = AutoScheduler.getAutoScheduler(mbox, getCalendarMailbox(ctxt), origInvites, this.mId, flags, tags, scidDefault, scidExceptions, replies, ctxt);
/*     */       
/* 583 */       if (autoScheduler == null) {
/* 584 */         newCalItem = mbox.setCalendarItem(ctxt.getOperationContext(), this.mId, flags, tags, scidDefault, scidExceptions, replies, 0L);
/*     */       }
/*     */       else
/*     */       {
/* 588 */         newCalItem = autoScheduler.doSchedulingActions();
/*     */       }
/* 590 */       if (newCalItem == null) {
/* 591 */         throw new DavException("cannot create icalendar item - corrupt ICAL?", 500);
/*     */       }
/*     */       
/* 594 */       if ((!uid.equals(name)) && 
/* 595 */         (acceptableClientChosenBasename)) {
/* 596 */         DavNames.put(DavNames.DavName.create(this.mMailboxId, newCalItem.getFolderId(), baseName), newCalItem.getId());
/*     */       }
/*     */       
/*     */ 
/* 600 */       return new CalendarObject.LocalCalendarObject(ctxt, newCalItem, isNewItem);
/*     */     } catch (BadOrganizerException.DiffOrganizerInComponentsException e) {
/* 602 */       throw new DavException.NeedSameOrganizerInAllComponents(e.getMessage());
/*     */     }
/*     */     catch (BadOrganizerException e)
/*     */     {
/* 606 */       throw new DavException(e.getMessage(), 403, e);
/*     */     } catch (ServiceException e) {
/* 608 */       if (e.getCode().equals("service.FORBIDDEN")) {
/* 609 */         throw new DavException(e.getMessage(), 403, e);
/*     */       }
/* 611 */       throw new DavException("cannot create icalendar item", 500, e);
/*     */     }
/*     */   }
/*     */   
/*     */   private String urlForCalendarItem(String user, String basename) throws DavException, ServiceException
/*     */   {
/* 617 */     StringBuilder url = new StringBuilder();
/* 618 */     url.append(DavServlet.getDavUrl(user)).append(this.mPath).append("/").append(basename);
/* 619 */     return url.toString();
/*     */   }
/*     */   
/*     */   private String defaultUrlForCalendarItem(String user, String uid) throws DavException, ServiceException {
/* 623 */     StringBuilder basename = new StringBuilder(uid).append(".ics");
/* 624 */     return urlForCalendarItem(user, basename.toString());
/*     */   }
/*     */   
/*     */   public String getFreeBusyReport(DavContext ctxt, Range.TimeRange range) throws ServiceException, DavException
/*     */   {
/* 629 */     Mailbox mbox = getCalendarMailbox(ctxt);
/* 630 */     FreeBusy fb = mbox.getFreeBusy(ctxt.getOperationContext(), range.getStart(), range.getEnd(), -1);
/* 631 */     return fb.toVCalendar(FreeBusy.Method.REPLY, ctxt.getAuthAccount().getName(), mbox.getAccount().getName(), null);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/resource/CalendarCollection.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */